package com.crm.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.Company;
import com.crm.model.dto.*;
import com.crm.model.query.CompanyQuery;
import com.crm.model.vo.CompanyVO;
import com.crm.service.ICompanyService;
import com.crm.uilt.ResultInfo;
import com.crm.uilt.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 公司 前端控制器
 * </p>
 *
 * @author zjd
 * @since 2021-04-01
 */
@Api("公司相关操作")
@RestController
@RequestMapping("/v1.0/user/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;

    @ApiOperation("根据id删除公司 级联删除 附件，联系人，交易，任务 中的 公司id")
    @DeleteMapping("/{id}")
    public ResponseEntity deleteCompanyById(@PathVariable("id") Long companyId ){
        /*if (ObjectUtil.isEmpty(companyId)){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }*/
        Boolean resut = companyService.deleteCompanyById(companyId);
        /*if (resut){
            return new ResultInfo(Status.SUCCESS);
        }*/
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("添加公司信息")
    @PostMapping("")
    public ResponseEntity<Company> addCompany(@RequestBody  CompanyDTO dto){
        Company resut = companyService.addCompany(dto);
        return new ResponseEntity<>(resut,HttpStatus.CREATED);
    }

    @ApiOperation("更新公司信息")
    @PutMapping("/{company-id}")
    public ResponseEntity updateCompany(@RequestBody @Validated CompanyDTO dto,@PathVariable("company-id") Long companyId){
        if (ObjectUtil.isEmpty(companyId)){
            new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        dto.setId(companyId);
        /*if (ObjectUtil.isEmpty(dto) || ObjectUtil.isEmpty(dto.getId()) || dto.getId()==0L ){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }*/
        Company resut = companyService.updateCompany(dto);
      /*  if (resut){
            return new ResultInfo(Status.SUCCESS);
        }*/
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("为公司添加母公司或者子公司 type 1 添加母公司 2 添加子公司")
    @PutMapping("/add-company-appertain")
    public   ResponseEntity<Void>  addCompanyAppertain(@RequestBody @Validated CompanyAppertainDTO dto){
        /*if (ObjectUtil.isEmpty(dto.getCompanyId()) || ObjectUtil.isEmpty(dto.getTargetCompanyId()) || ObjectUtil.isEmpty(dto.getType())){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }*/
        Boolean resut = companyService.addCompanyAppertain(dto.getCompanyId(), dto.getTargetCompanyId(), dto.getType());
        /*if (resut){
            return new ResultInfo(Status.SUCCESS);
        }*/
        return new ResponseEntity(HttpStatus.OK);
    }


    @ApiOperation("根据id查询公司详细信息")
    @GetMapping("/{company-id}")
    public ResponseEntity<CompanyVO> findCompanyInfoById(@PathVariable("company-id") Long companyId){
        /*if (ObjectUtil.isEmpty(companyId)){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }*/
        CompanyVO vo = companyService.findCompanyInfoById(companyId);
        return new ResponseEntity(vo,HttpStatus.OK);
    }

    @ApiOperation("查询公司列表")
    @GetMapping("")
    public ResponseEntity<Page<Company>> listCompany(@ModelAttribute CompanyQuery companyQuery,
                                                     @RequestParam("size") Integer size,
                                                     @RequestParam("current") Integer current ){
        if (ObjectUtil.isEmpty(size) || size==0){
            size=10;
        }
        Page<Company> companies = companyService.listCompany(companyQuery, current, size);
        //Page<Company> companies = companyService.listCompany(companyQuery,pageable);
        return new ResponseEntity(companies,HttpStatus.OK);
    }

    @ApiOperation("添加公司关联信息 - 联系人")
    @PostMapping("/add-company-contact")
    public ResultInfo addCompanyContact(@RequestBody ContactDTO contactDTO){
        if (ObjectUtil.isEmpty(contactDTO) || ObjectUtil.isEmpty(contactDTO.getCompanyId()) || contactDTO.getCompanyId()==0L){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }
        Boolean resut=companyService.addCompanyContact(contactDTO);
        if (resut){
            return new ResultInfo(Status.SUCCESS);
        }
        return new ResultInfo(Status.WARN);
    }

    @ApiOperation("将公司指派给用户")
    @PutMapping("/assign")
    public ResponseEntity<Void> assignCompanyForUser(@RequestBody @Validated  CompanyForUserDTO dto){
        /*if ((ObjectUtil.isEmpty(dto.getUserId()) || dto.getUserId()==0L) || (ObjectUtil.isEmpty(dto.getCompanyId()) || dto.getCompanyId()==0L)){
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR);
        }*/
        Boolean resut=companyService.assignCompanyForUser(dto.getUserId(),dto.getCompanyId());

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("公司合并")
    @PutMapping("/merge")
    public ResponseEntity<Void> mergeCompany( @RequestBody @Validated  MergeCompanyDTO dto) throws Exception {
        companyService.mergeCompany(dto.getTargetCompanyId(),dto.getCompanyId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("添加公司任务 -（notes，emails，calls，tasks）")
    public ResultInfo addCompanyAct(){
        return new ResultInfo();
    }








}
